package com.chc.web.house_list.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chc.utils.Msg;
import com.chc.web.house_building.entity.HouseBuilding;
import com.chc.web.house_building.service.HouseBuildingService;
import com.chc.web.house_list.entity.HouseList;
import com.chc.web.house_list.entity.ListParm;
import com.chc.web.house_list.service.HouseListService;
import com.chc.web.house_unit.entity.HouseUnit;
import com.chc.web.house_unit.service.HouseUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/01/13/10:43
 * @Description: 房屋列表控制器
 */

@RestController
@RequestMapping("/api/houseList")
public class HouseListController {
    @Autowired
    private HouseListService houseListService;

    @Autowired
    private HouseUnitService houseUnitService;

    @Autowired
    private HouseBuildingService houseBuildingService;

    /**
     * 获取房屋列表
     * */
    @GetMapping("/list")
    public Msg getList(ListParm parm){
        IPage<HouseList> list = houseListService.getList(parm);
        return Msg.success("查询成功",list);
    }

    /**
     * 新增房屋
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:house:add')")
    public Msg add(@RequestBody HouseList houseList){
        List<HouseList> list = houseListService.getHouseList();
        for (HouseList item :list){
            if (item.getHouseNum().equals(houseList.getHouseNum())){
                return Msg.error("房屋已存在,不能重复操作!");
            }
        }
        boolean save = houseListService.save(houseList);
        if(save){
            return Msg.success("新增房屋成功!");
        }
        return Msg.error("新增房屋失败!");
    }

    /**
     * 编辑房屋
     */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:house:edit')")
    public Msg edit(@RequestBody HouseList houseList){
        List<HouseList> list = houseListService.getHouseList();
        for (HouseList item :list){
            if (item.getHouseArea().equals(houseList.getHouseArea())){
                if (item.getHouseNum().equals(houseList.getHouseNum())){
                    return Msg.error("房屋已存在,不能重复操作!");
                }
            }
        }
        boolean save = houseListService.updateById(houseList);
        if(save){
            return Msg.success("编辑房屋成功!");
        }
        return Msg.error("编辑房屋失败!");
    }

    /**
     * 删除房屋
     */
    @DeleteMapping("/{houseId}")
    @PreAuthorize("hasAuthority('sys:house:delete')")
    public Msg edit(@PathVariable("houseId") String houseId){
        HouseList houseList = houseListService.getById(houseId);
        if (houseList.getStatus().equals("1")){
            return Msg.error("该房屋被使用中,不可以删除");
        }
        boolean save = houseListService.removeById(houseId);
        if(save ){
            return Msg.success("删除房屋成功!");
        }
        return Msg.error("删除房屋失败!");
    }

    /**
     * 根据单元id获取房屋列表
     * */
    @GetMapping("/getHouseByUnitId")
    public Msg getHouseByUnitId(HouseList houseList){
        QueryWrapper<HouseList> query = new QueryWrapper();
        query.lambda().eq(HouseList::getUnitId,houseList.getUnitId());
        List<HouseList> list = houseListService.list(query);
        return Msg.success("查询成功",list);
    }
}
